<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box{
            width: 500px; margin: 30px auto;
        }
        #btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div id="box">
        <input type="text" id="num" value="2,4,6">输入数字求和，数字之间用‘,’隔开
        <br>
        <input id="btn" type="button" value="求和">
        <strong id="sun"></strong>
    </div>
</body>
<script>
    window.onload= function(){
        var oBtn = document.getElementById('btn')
        var oNum = document.getElementById('num')
        var oSun = document.getElementById('sun')
        oNum.onkeyup = function(){
            this.value = this.value.replace(/[^(\d)|(,)]/,"")
        }
        oBtn.onclick = function(){
            var sun = 0 
            var oNum = document.getElementById('num').value.split(',')
            for(var i in oNum){
                sun += parseInt(oNum[i])
            }
            oSun.innerHTML = sun
        }
    }
</script>
</html>